package com.orcamento.entity.report.subreport;

import java.util.List;

import com.orcamento.entity.Gain;
import com.orcamento.entity.Spent;
import com.orcamento.entity.enums.Month;
import com.orcamento.util.Money;

public class MonthBalance implements Comparable<MonthBalance> {

	private Month month;
	private Integer year;
	private Money balance = new Money();
	
	public MonthBalance(Integer month, Integer year, List<Gain> gains, List<Spent> spents) {
		this.month = Month.findById(month);
		this.year = year;
		generateBalance(gains, spents);
	}

	private void generateBalance(List<Gain> gains, List<Spent> spents) {
		for(Gain gain : gains) {
			balance = balance.plus( gain.getValue() );
		}
		for(Spent spent : spents) {
			balance = balance.minus( spent.getValue() );
		}
	}

	public Month getMonth() {
		return month;
	}

	public Integer getYear() {
		return year;
	}

	public Money getBalance() {
		return balance;
	}

	@Override
	public int compareTo(MonthBalance other) {
		if(other == null) {
			return 1;
		}
		if(balance.equals(other.balance)) {
			return 0;
		}
		return balance.greaterThan(other.balance) ? 1 : -1 ;

	}

	@Override
	public String toString() {
		return "MonthBalance [month=" + month + ", year=" + year + ", balance=" + balance + "]";
	}
	
}
